<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="/public/javascripts/jquery-1.7.1.min.js" charset="UTF-8"></script>
    <link href="/public/stylesheets/templates_01.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<table width="100%" border="0" cellpadding="0" class="alltablebg">

    <tr>
        <td class="tdtitle" valign="center" style="text-align: center">ID</td>
        <td class="tdtitle" valign="center" style="text-align: center">名称</td>
        <td class="tdtitle" valign="center" style="text-align: center">答案</td>
        <td class="tdtitle" valign="center" style="text-align: center">等级</td>
        <td class="tdtitle" valign="center" style="text-align: center">状态</td>
        <td class="tdtitle" valign="center" style="text-align: center">操作</td>
    </tr>

    <tr>
        <form id="brandForm" action="/question/save" onsubmit="if(jQuery.trim($('#addName').val())==''){alert('不能为空');$('#addName').focus();return false;}return true;" method="post">
            <td class="tdtitle" valign="center" style="text-align: center">&nbsp;</td>
            <td class="tdtitle" valign="center" style="text-align: center"><textarea id="addName" type="text" name="name" cols="20" rows="3"></textarea></td>
            <td class="tdtitle" valign="center" style="text-align: center">&nbsp;</td>
            <td class="tdtitle" valign="center" style="text-align: center">&nbsp;</td>
            <td class="tdtitle" valign="center" style="text-align: center">未审核<input name="status" value="0" type="radio"> 审核<input name="status" value="1" checked type="radio"></td>
            <td class="tdtitle" valign="center" style="text-align: center"><input type="submit" value="添加"></td>
        </form>
    </tr>

    <tr>
        <td style="text-align: left" colspan="5">
            <br/>

            <form id="pageForm" action="/question/" method="get">
                ID:<input name="id" value="${params.id}"/>名称:<input name="name" value="${params.name}"/>
                审核状态:<select name="status">
                <option value="">所有</option>
                <option ${params.status == "0" ? "selected" : "" } value="0">未审核</option>
                <option ${params.status == "1" ? "selected" : "" } value="1">审核</option>
            </select>
                <input value="查询" type="submit"/>
            </form>
        </td>
    </tr>

#{list items:questionList, as:'question'}
    <tr>
        <form id="brandForm" action="/question/save" method="post">
            <td valign="center" style="text-align: center">${question.id}</td>
            <td valign="center" style="text-align: center"><textarea type="text" id="name_${question.id}" name="name" cols="20" rows="3">${question.name}</textarea><input type="button" value="ok" onclick="updateQuestion(${question.id},$('#name_${question.id}').val(),null)"></td>

            <td valign="center" style="text-align: center">
                <div>
                    %{
                        List <models.Answer> answers = models.Answer.find("from models.Answer where questionId = " + question.id ).fetch();
                    }%
                    #{list items:answers, as:'answer' }
                    ${answer_index}.<input id="_${answer.id}" name="name_question${question.id}" value="${answer.name}" type="text"/><input name="question_answer${question.id}" ${question.answerId == answer.id ?"checked":""} onclick="updateQuestionAnswer(${question.id},${answer.id});" value="${answer.id}" type="radio"/>
                       <br/>
                    #{/list}
                    <input type="button" value="保存" onclick="saveAnswerName('name_question${question.id}')"/>
                </div>


            </td>
            <td valign="center" style="text-align: center">
                0:<input ${question.grade == 0 || question.grade == null ?"checked":""} name="question_grade_${question.id}" value="0" type="radio" onclick="updateQuestionGrade(${question.id},0);"  />
                1:<input ${question.grade == 1?"checked":""} name="question_grade_${question.id}" value="0" type="radio" onclick="updateQuestionGrade(${question.id},1);" />
                2:<input ${question.grade == 2?"checked":""} name="question_grade_${question.id}" value="0" type="radio" onclick="updateQuestionGrade(${question.id},2);" /><br/>
                3:<input ${question.grade == 3?"checked":""} name="question_grade_${question.id}" value="0" type="radio" onclick="updateQuestionGrade(${question.id},3);" />
                4:<input ${question.grade == 4?"checked":""} name="question_grade_${question.id}" value="0" type="radio" onclick="updateQuestionGrade(${question.id},4);" />
                5:<input ${question.grade == 5?"checked":""} name="question_grade_${question.id}" value="0" type="radio" onclick="updateQuestionGrade(${question.id},5);"  />
            </td>
            <td valign="center" style="text-align: center">
                未审核<input name="status_${question.id}" value="0" ${question.status == 0 ?"checked":""} type="radio" onclick="updateQuestion(${question.id},null,0)">
                审核<input name="status_${question.id}" value="1" ${question.status == 1 ?"checked":""} type="radio" onclick="updateQuestion(${question.id},null,1) ">
            </td>
            <td valign="center" style="text-align: center">&nbsp;</td>
        </form>
    </tr>
#{/list}

</table>
<div class="titlebar">
    <div class="r">
    #{page list:questionList,form:'pageForm' /}
    </div>
</div>
</body>
<script>

    function updateQuestionGrade( questionid , grade ){

        $.ajax({
            url: '/question/updateQuestionGrade',
            type: 'POST',
            dataType: 'text',
            timeout: 1000,
            data: "questionid=" + questionid + "&grade=" + grade ,
            cache: false,
            async: false,
            error: function () {
                //alert('Error loading XML document');
            },
            success: function (text) {
                if (jQuery.trim(text) == "true") {
                    //alert("成功");
                } else {
                    alert("失败");
                }
            }
        });

    }

    function updateQuestionAnswer( questionid , answerid ){

        $.ajax({
            url: '/question/updateQuestionAnswer',
            type: 'POST',
            dataType: 'text',
            timeout: 1000,
            data: "questionid=" + questionid + "&answerid=" + answerid ,
            cache: false,
            async: false,
            error: function () {
                //alert('Error loading XML document');
            },
            success: function (text) {
                if (jQuery.trim(text) == "true") {
                    //alert("成功");
                } else {
                    alert("失败");
                }
            }
        });

    }

    function saveAnswerName(name) {
        var input = $("input[name='" + name + "']");
        var id = "";
        var name = "";

        for (var i = 0; i < input.length; i++) {
            var _id = input.get(i).id.toString();
            var _name = input.get(i).value.toString();
            id += "&id=" + _id.substr(1,_id.length);
            name += "&name=" + _name;
        }

        $.ajax({
            url: '/question/updateAnswerName',
            type: 'POST',
            dataType: 'text',
            timeout: 1000,
            data: "1=1" + id + name ,
            cache: false,
            async: false,
            error: function () {
                //alert('Error loading XML document');
            },
            success: function (text) {
                if (jQuery.trim(text) == "true") {
                    //alert("成功");
                } else {
                    alert("失败");
                }
            }
        });

    }

    function updateQuestion(id, name, status) {

        var data = "id=" + id;

        if (name != null) {
            data += "&name=" + name;
        }

        if (status != null) {
            data += "&status=" + status;
        }

        $.ajax({
            url: '/question/update',
            type: 'POST',
            dataType: 'text',
            timeout: 1000,
            data: data,
            cache: false,
            async: false,
            error: function () {
                //alert('Error loading XML document');
            },
            success: function (text) {
                if (jQuery.trim(text) == "true") {
                    //alert("成功");
                } else {
                    alert("失败");
                }
            }
        });

    }

</script>
</html>